﻿function goTo(url)
{
    mainWindow = top.frames["main"];

    if(mainWindow)
        mainWindow.location = url.toString();
    else
        window.location = url.toString();
}

function goToIn(url, target)
{
    window.open(url.toString(), target);
}

function alert(message) 
{
    error(message);
}

function wait(message)
{
    turnOff();
    return Common.wait(message, "floatingWindow", "turnOn()");
}
        
function error(message)
{
    var func;

    turnOff();
    
    if( (message.name) && (message.name == "Pampa.CRUD.Exceptions.SessionExpiratedException") ) 
        func = "turnOn(); goHome();";
    else if( (!message.name) && (message.indexOf("SessionExpiratedException") != -1) )
        func = "turnOn(); goHome();";
    else
        func = "turnOn();";
    
    return Common.alert("ERROR", message, "Accept", "floatingWindow", func);
}

function warning(message)
{
    turnOff();
    return Common.alert("ADVERTENCIA", ParseControlledException(message), "Accept", "floatingWindow", "turnOn()");
}

function info(message, onAccept)
{
    turnOff();
    return Common.alert("INFORMACION", message, "Accept", "floatingWindow", "turnOn()", onAccept);
}

function confirm(message, onAccept)
{
    turnOff();
    return Common.confirm("", message, "Accept", "Cancel", "floatingWindow", "turnOn()", onAccept);
}       

function turnOff()
{
    // TODO: apagar otros frames
}

function turnOn()
{
    // TODO: apagar otros frames
}

function ParseControlledException(message)
{
    if(message.description) {
        return message.description;
    }
    else {
    
        var pos = message.indexOf("]");
        if(pos!=-1)
            message = message.substring(pos+1, message.length);
            
        pos = message.indexOf("EntityManagerException");
        if(pos!=-1)
            message = message.substring(pos+23, message.length);
        
        pos = message.indexOf("ValidationFailure");
        if(pos!=-1)
            message = message.substring(pos+18, message.length);
         
        pos = message.indexOf("EntityValidationException");
        if(pos!=-1)
            message = message.substring(pos+26, message.length);        
    }
    
    return message;
}